package com.jari.lg.service.analysis.device;

import com.jari.lg.domain.DevBase;
import com.jari.lg.domain.DevErrRt;
import com.jari.lg.mapper.DevErrRtMapper;
import com.jari.lg.utils.BaseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

@Service
public class DevErrRtService {

    @Resource
    private DevErrRtMapper devErrRtMapper;

    @Scheduled(cron = "0 0/5 * * * ? ")
    private void createDevErrRt() {
        List<DevErrRt> ans = new ArrayList<>();
        List<DevBase> devBaseList = BaseData.devBaseList;
        Integer random = new Random().nextInt(100);
        for (DevBase d : devBaseList) {
            DevErrRt devErrRt = new DevErrRt();
            devErrRt.setDeviceid(d.getDevicecode());
            devErrRt.setDevicetype(d.getDevicetype());
            devErrRt.setDevicename(d.getDevicename());
            if (!d.getCrosscode().isEmpty()) {
                devErrRt.setRoadid(d.getCrosscode());
                devErrRt.setRoadname(BaseData.crossBaseList.stream().filter(item -> item.getCrosscode().equals(d.getCrosscode())).collect(Collectors.toList()).get(0).getCrossname());
            }
            //暂时没有路段数据
            if (!d.getCrossroadcode().isEmpty()) {

            }
            //10%的概率不故障
            devErrRt.setDevicestate((random < 10 ? 1 : 0));
            devErrRt.setDatetime(new Date());
            ans.add(devErrRt);
        }
        devErrRtMapper.insertBatch(ans);
    }
}
